set more off
clear all
cap log close
cd "~/Dropbox/Farm Subsidies and Committees/analyses/final_tables"
do table_prog.do

use if election_year_desc >= 2009 using ../collapsed, clear
drop if any_neg == 1

* mark orgs
merge m:1 customer_code using ../org_ids, keep(1 3)
gen org = (_merge == 3)

* rescale dvs
gen voted_rescaled = voted*100
gen ran_rescaled = ran*100
gen winner_rescaled = winner*100

label var voted_rescaled "Voted $\times$ 100"
label var ran_rescaled "Ran $\times$ 100"
label var winner_rescaled "Won $\times$ 100"

* binary payments
gen pos_amt = amt_all > 0 
gen pos_amt_lag1 = amt_all_lag1 > 0
gen pos_amt_lead1 = amt_all_lead1 > 0

label var pos_amt "Received payment"
label var pos_amt_lag1 "Received payment (lagged)"
label var pos_amt_lead1 "Received payment (lead)"

* log payments
gen log_amt = log(amt_all + 1)
gen log_amt_lag1 = log(amt_all_lag1 + 1)
gen log_amt_lead1 = log(amt_all_lead1 + 1)

label var log_amt "Log payment + 1"
label var log_amt_lag1 "Log payment + 1 (lagged)"
label var log_amt_lead1 "Log payment + 1 (lead)"


// Table OA1
eststo clear

eststo: areg voted_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa1.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA2
eststo clear

eststo: areg voted_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa2.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


drop if org == 1


// Table OA3
eststo clear

eststo: areg voted_rescaled pos_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled pos_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled pos_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled pos_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled pos_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled pos_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa3.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA4
eststo clear

eststo: areg voted_rescaled log_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled log_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled log_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled log_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled log_amt_lag1 if election_year_desc >= 2010, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled log_amt_lag1 i.election_year_desc if election_year_desc >= 2010, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa4.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA5
eststo clear

eststo logit1: logit voted pos_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin1: margins, dydx(pos_amt) post

eststo logit2: logit ran pos_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin2: margins, dydx(pos_amt) post

eststo logit3: logit winner pos_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin3: margins, dydx(pos_amt) post

esttab logit1 margin1 logit2 margin2 logit3 margin3 using table_oa5.tex, se label drop(*election_year*) stats(ymean yfe ffe, label("Mean of outcome (\%)" "Year FE" "Farmer FE")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Logit" "Marginal" "Logit" "Marginal" "Logit" "Marginal") collabels(none) compress booktabs frag float width(2\hsize) replace


// Table OA6
eststo clear

eststo logit1: logit voted log_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin1: margins, dydx(log_amt) post

eststo logit2: logit ran log_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin2: margins, dydx(log_amt) post

eststo logit3: logit winner log_amt i.election_year_desc, cluster(customer_code) iterate(4)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace
eststo margin3: margins, dydx(log_amt) post

esttab logit1 margin1 logit2 margin2 logit3 margin3 using table_oa6.tex, se label eqlabels(none) drop(*election_year*) stats(ymean yfe ffe, label("Mean of outcome (\%)" "Year FE" "Farmer FE")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Logit" "Marginal" "Logit" "Marginal" "Logit" "Marginal") collabels(none) compress booktabs frag float width(2\hsize) replace


// Table OA7
table_prog_xtlogit, rhsvars(pos_amt) filename(table_oa7)


// Table OA8
table_prog_xtlogit, rhsvars(log_amt) filename(table_oa8)


// Table OA9
eststo clear

eststo: areg voted_rescaled pos_amt pos_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled pos_amt pos_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled pos_amt pos_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled pos_amt pos_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled pos_amt pos_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled pos_amt pos_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa9.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA10
eststo clear

eststo: areg voted_rescaled log_amt log_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled log_amt log_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled log_amt log_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled log_amt log_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled log_amt log_amt_lead1, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled log_amt log_amt_lead1 i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa10.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA13
eststo clear

eststo: areg winner_served_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_served_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg served_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg served_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa13.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Won and served" "Served", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA14
eststo clear

eststo: areg winner_served_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_served_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg served_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg served_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa14.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Won and served" "Served", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


cap egen avg_amt_all = mean(amt_all), by(customer_code)


// Table OA15
table_prog_strat, dv(voted_rescaled) rhsvars(pos_amt) filename(table_oa15)


// Table OA16
table_prog_strat, dv(ran_rescaled) rhsvars(pos_amt) filename(table_oa16)


// Table OA17
table_prog_strat, dv(winner_rescaled) rhsvars(pos_amt) filename(table_oa17)


gen pos_amt_prc = amt_prc > 0 
gen pos_amt_con = amt_con > 0 
gen log_amt_prc = log(amt_prc + 1)
gen log_amt_con = log(amt_con + 1)


// Table OA18
table_prog, rhsvars(pos_amt_prc) filename(table_oa18)


// Table OA19
table_prog, rhsvars(log_amt_prc) filename(table_oa19)


// Table OA20
table_prog, rhsvars(pos_amt_con) filename(table_oa20)


// Table OA21
table_prog, rhsvars(log_amt_con) filename(table_oa21)



use if election_year_desc >= 2009 & any_neg != 1 & uncontested != 1 using ../merged, clear
collapse (max) voted ran winner served (first) amt_all amt_all_lag1 amt_all_lead1 amt_dis amt_dis_lag1 amt_dis_lead1, by(customer_code election_year_desc) fast

* drop orgs
merge m:1 customer_code using ../org_ids, keep(1) keepusing(customer_code) nogen

* rescale dvs
gen voted_rescaled = voted*100
gen ran_rescaled = ran*100
gen winner_rescaled = winner*100

label var voted_rescaled "Voted $\times$ 100"
label var ran_rescaled "Ran $\times$ 100"
label var winner_rescaled "Won $\times$ 100"

* binary payments
gen pos_amt = amt_all > 0 
label var pos_amt "Received payment"


// Table OA11
eststo clear

eststo: areg voted_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa11.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


// Table OA12
eststo clear

eststo: areg voted_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_oa12.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace
